# redis 
import redis
# 创建连接对象
r = redis.Redis(
    host='192.168.88.11',
    port='6379',
    db=0
)
# 存储字符串
r.set('name', 'tom')
# 获取字符串 字节串转字符串
s = r.get('name')
print(s, str(s, encoding='utf8'))

# 如果不存在，才会存储成功;存在不会再去存储
r.setnx('gender', 'jurry')
print(r.get('gender'))
# 设置有效期（键，有效期，值）
r.setex('title', 20, 'python')
print(r.get('title'))
# 哈希表 键值对格式
r.hset('person', 'name', 'tom')
r.hset('person', 'age', 18)
r.hset('person', 'gender', 'boy')
# 获取
print(r.hget('person', 'name'))
print(r.hget('person', 'age'), r.hget('person', 'gender'))
# 获取所有 返回字典
print(r.hgetall('person'))
dict01 = r.hgetall('person')
dict02 = {}
for key in dict01:
    print(key, dict01[key])
for key, value in dict01.items():
    print(key, value)
    key = str(key, encoding='utf-8')
    value = str(value, encoding='utf-8')
    # 放到字典
    dict02[key] = value    
print(dict02)
print(dict02['name'])

# 关闭连接
r.close()
